package cn.waka.sys.admin.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import cn.waka.sys.admin.object.Box;
import cn.waka.sys.admin.object.Page;

@Component
public class DeviceDao extends BaseDao{
	
	public List<Box> findBoxList(Page p,Map<String,String> param){
		int page=p.getPage();
		int start=(page-1)*p.getSize();
		
		String sql="select box_id,device_id,device_num,channel from b_box where 1=1 ";
		Set<String> pks= param.keySet();
		Object[] dbparams=new Object[2+param.size()];
		int i=0;
		if(param.size()>0){
			for(String pa : pks){
				sql+=" and "+pa+" like ?";
				dbparams[i]=param.get(pa);
				i++;
			}
			
			dbparams[param.size()]=start;
			dbparams[param.size()+1]=p.getSize();
		}else{
			dbparams[0]=start;
			dbparams[1]=p.getSize();
		}
		sql+=" limit ?,?";
		List<Box> list=this.query(sql, new RowMapper<Box>(){
			@Override
			public Box mapRow(ResultSet rs, int rowNum) throws SQLException {
				Box b=new Box();
				b.setId(rs.getInt(1));
				b.setDeviceId(rs.getString(2));
				b.setDeviceNum(rs.getInt(3));
				b.setChannel(rs.getString(4));
				return b;
			}
			
		},dbparams);
		return list;
	}
	
	public int findBoxCount(Page p,Map<String,String> param){
		String sql="select count(1) from b_box where 1=1 ";
		Set<String> pks= param.keySet();
		Object[] dbparams=new Object[param.size()];
		int i=0;
		if(param.size()>0){
			for(String pa : pks){
				sql+=" and "+pa+" like ?";
				dbparams[i]=param.get(pa);
				i++;
			}
		}
		int c=0;
		if(param.size()>0){
			c=this.queryForObject(sql,Integer.class,dbparams);
		}else{
			c=this.queryForObject(sql,Integer.class);
		}
		return c;
	}
	
	
}
